# 统计项目有效代码行数

import os
import re
def count_lines_in_file(file_path):
    with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
        lines = f.readlines()

    # 正则表达式用于匹配注释和空行
    comment_regex = re.compile(r'^\s*#')
    blank_line_regex = re.compile(r'^\s*$')

    count = 0
    in_multiline_comment = False

    for line in lines:
        line = line.strip()

        if in_multiline_comment:
            if line.endswith('"""') or line.endswith("'''"):
                in_multiline_comment = False
            continue
        elif line.startswith('"""') or line.startswith("'''"):
            if line.endswith('"""') or line.endswith("'''"):
                continue
            in_multiline_comment = True
            continue

        if not comment_regex.match(line) and not blank_line_regex.match(line):
            count += 1

    return count


def count_lines_in_directory(directory):
    total_lines = 0
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith('.py'):  # 只统计 Python 文件
                file_path = os.path.join(root, file)
                total_lines += count_lines_in_file(file_path)
    return total_lines


if __name__ == "__main__":
    project_directory = '.'  # 当前目录
    total_lines = count_lines_in_directory(project_directory)
    print(f"总有效代码行数: {total_lines}")